package filter;


        import pojo.User;

        import javax.servlet.*;
        import javax.servlet.annotation.WebFilter;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpSession;
        import java.io.IOException;

@WebFilter("/*" )
public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException, IOException {

        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String requestURL = req.getRequestURL().toString();
        System.out.println("url");
        if(requestURL.contains("/login") || requestURL.contains("login.html")){
            filterChain.doFilter(servletRequest,servletResponse);
            System.out.println("fa");
            return;
        }
        System.out.println("结束");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user!=null){
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
            req.getRequestDispatcher("/login.html").forward(servletRequest,servletResponse);
        }

    }

    @Override
    public void destroy() {

    }
}
